package com.eliansun.crawler.pageparse.beijing;

import com.alibaba.fastjson.JSONObject;
import com.eliansun.crawler.pageparse.AbstractPageProcess;
import com.eliansun.crawler.spider.MySpider;
import com.eliansun.util.HttpClient;
import com.eliansun.util.SpiderUtil;
import com.eliansun.util.WebDriverPath;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liujin
 * @description 京东解析模板
 * @create 2019/7/11
 */
public class JD extends AbstractPageProcess {

    int pageCount = 1;
    int pageNum = 1;
    int countnNum = 0;

    private static final Logger LOGGER = LoggerFactory.getLogger(JD.class);

    private Site site = Site.me().setCycleRetryTimes(5).setRetryTimes(5).setSleepTime(200).setTimeOut(3 * 60 * 1000)
            .setUserAgent("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36")
            .addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
            .addHeader("Accept-Language", "zzh-CN,zh;q=0.8")
            .addHeader("Accept-Encoding", "zh-CN,zh;q=0.8");

    @Override
    public void process(Page page) {
        String url = page.getUrl().toString();
//        详情页
        if (
                page.getUrl().regex("//item.yiyaojd.com/(\\d*).html").match() ||
                        page.getUrl().regex("//item.jd.com/(\\d*).html").match() ||
                        page.getUrl().toString().indexOf("https://ccc-x.jd.com/dsp") != -1
        ) {
            String goodsUrl = page.getUrl().toString();

            List<String> ptTableDt = page.getHtml().xpath("//div[@class='Ptable-item']//dl//dt/text()").all();
            List<String> ptTableDd = page.getHtml().xpath("//div[@class='Ptable-item']//dl//dd/text()").all();
            Map<String, Object> resultMap = new HashMap<>();
            String productCompany = "";
            String medicalDeviceNo = "";
            String spec = "";
            if (ptTableDd != null && ptTableDd != null && ptTableDd.size() == ptTableDt.size()) {
                for (int i = 0; i < ptTableDt.size(); i++) {
                    if (ptTableDt.get(i).equals("生产企业")) {
                        productCompany = ptTableDd.get(i);
                    }
                    if (ptTableDt.get(i).equals("医疗器械注册证编号") || ptTableDt.get(i).equals("医疗器械注册证编号或者备案凭证编号") || ptTableDt.get(i).equals("批准文号")) {
                        medicalDeviceNo = ptTableDd.get(i);
                    }
                    if (ptTableDt.get(i).equals("产品规格")) {
                        spec = ptTableDd.get(i);
                    }
                }
            }

            //价格
            String str[] = goodsUrl.split(".html");
            String str1 = str[0];
            String productId = str1.substring(str1.lastIndexOf("/") + 1);
            String priceStr = HttpClient.get("https://c0.3.cn/stock?skuId=" + productId + "&area=1_72_2799_0&venderId=746652&cat=9192,9197,12188&buyNum=1&choseSuitSkuIds=&extraParam={%22originid%22:%221%22}&ch=1&fqsp=0&pduid=12476862&pdpin=&detailedAdd=null&callback=jQuery7843214", SpiderUtil.getPcHeaders(url));;
            priceStr = priceStr.substring(priceStr.indexOf("(") + 1, priceStr.lastIndexOf(")"));
            priceStr = JSONObject.parseObject(priceStr).get("stock").toString();
            priceStr = JSONObject.parseObject(priceStr).get("jdPrice").toString();
            String originalPrice = JSONObject.parseObject(priceStr).get("p").toString();
            //产品名
            String goodsName = page.getHtml().xpath("//div[@class='sku-name']//text()").toString();
            String brand = page.getHtml().xpath("//ul[@id='parameter-brand']//li//@title").toString();

            resultMap.put("spec", spec);
            resultMap.put("medicalDeviceNo", medicalDeviceNo);
            resultMap.put("productCompany", productCompany);
            resultMap.put("price",originalPrice);
            resultMap.put("brand",brand);
            resultMap.put("productName",goodsName);
            resultMap.put("executiveStandard","");
            resultMap.put("nationality","");
            resultMap.put("store",super.cTerrace);
            resultMap.put("url",goodsUrl);
            resultMap.put("version","");
            page.putField("result", resultMap);
            countnNum++;
            System.out.println(countnNum);
        } else { //搜索页
            LOGGER.info("这是第" + pageNum + "页数据");
            WebDriver webDriver = WebDriverPath.getWebDriver(url);
            try {
                ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//滚动条一致底部
                Thread.sleep(3000);
                ((JavascriptExecutor) webDriver).executeScript("window.scrollTo(0, 10000)");//防止懒加
                Thread.sleep(800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            String html = webDriver.getPageSource();
            webDriver.close();
            String noGoods = Jsoup.parse(html).select("div[class=check-error]").text();
            if(noGoods.contains("没有找到")){
                return;//无数据
            }
            Elements elementsGoods = Jsoup.parse(html).select("div[id=J_goodsList]").select("ul").select("li");
            pageCount = pageCount + 2;
            List<String> detail = new ArrayList<>();
            for (Element elementsGood : elementsGoods) {
                detail.add(elementsGood.select("div").select("div").select("a").attr("href"));
            }
            page.addTargetRequests(detail);

            pageNum++;
            //下一页
            String nextPage = url.substring(0,url.indexOf("page")) + "page=" + pageCount;
            page.addTargetRequest(nextPage);
        }
    }

    @Override
    public Site getSite() {
        return site;
    }

    public static void main(String[] args) {
        MySpider spider = new MySpider();
        JD jd = new JD();

        jd.setMySpider(spider);
        spider.setPageProcessor(jd);
//        spider.addUrl("https://item.jd.com/1242256.html");
        spider.addUrl("https://search.jd.com/Search?keyword=%E9%92%99%E5%B0%94%E5%A5%87&enc=utf-8&page=1");
        spider.thread(10);
        spider.run();

//        https://search.jd.com/Search?keyword=@key@enc=utf-8
    }
}